package com.katze.boot.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.katze.boot.system.dao.DeptMapper;
import com.katze.boot.system.domain.Dept;
import com.katze.boot.system.service.DeptService;
import com.katze.common.tree.Node;
import com.katze.common.tree.TreeNode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service("deptService")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DeptServiceImpl extends ServiceImpl<DeptMapper, Dept> implements DeptService {


    @Override
    public Map<String, Object> findDepts(Dept dept) {
        Map<String, Object> result = new HashMap<>();
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        boolean condition = findDeptCondition(wrapper, dept);
        List<Dept> depts = this.baseMapper.selectList(wrapper);

        List<TreeNode> nodes = depts.stream().map(item -> {
            TreeNode node = new TreeNode(item.getDeptId(), item.getDeptName());
            node.setPid(item.getParentId());
            node.setCreateTime(item.getCreateTime());
            node.setModifyTime(item.getModifyTime());
            node.setOrder(item.getOrderNum().intValue());
            return node;
        }).collect(Collectors.toList());

        if (condition){
            result.put("rows", nodes);
            result.put("total", depts.size());
        } else {
            result.put("rows", Node.build(nodes));
            result.put("total", 1);
        }
        return result;
    }

    @Override
    public List<Dept> findDeptList(Dept dept) {
        LambdaQueryWrapper<Dept> wrapper = new LambdaQueryWrapper<>();
        findDeptCondition(wrapper, dept);
        return this.baseMapper.selectList(wrapper);
    }

    private boolean findDeptCondition(LambdaQueryWrapper<Dept> queryWrapper, Dept dept) {
        boolean condition = false;
        if (StringUtils.isNotBlank(dept.getDeptName())) {
            queryWrapper.like(Dept::getDeptName, dept.getDeptName());
            condition = true;
        }

        if (StringUtils.isNotBlank(dept.getCreateTimeFrom()) && StringUtils.isNotBlank(dept.getCreateTimeTo())) {
            queryWrapper.ge(Dept::getCreateTime, dept.getCreateTimeFrom())
                    .le(Dept::getCreateTime, dept.getCreateTimeTo())
                    .orderByAsc(Dept::getOrderNum);
            condition = true;
        }
        return condition;
    }

    @Override
    @Transactional
    public void createDept(Dept dept) {
        Long parentId = dept.getParentId();
        if (parentId == null)
            dept.setParentId(0L);
        dept.setCreateTime(new Date());
        this.save(dept);
    }

    @Override
    @Transactional
    public void updateDept(Dept dept) {
        dept.setModifyTime(new Date());
        this.baseMapper.updateById(dept);
    }

    @Override
    @Transactional
    public void deleteDepts(String[] deptIds) {
        this.delete(Arrays.asList(deptIds));
    }

    private void delete(List<String> deptIds) {
        removeByIds(deptIds);

        LambdaQueryWrapper<Dept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Dept::getParentId, deptIds);
        List<Dept> depts = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isNotEmpty(depts)) {
            List<String> deptIdList = new ArrayList<>();
            depts.forEach(d -> deptIdList.add(String.valueOf(d.getDeptId())));
            this.delete(deptIdList);
        }
    }
}